package panel.eating
{
	import com.greensock.TweenLite;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.events.TransformGestureEvent;
	import flash.geom.Rectangle;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import main.ClientConstants;
	
	import panel.home.fastBtn.MoreFastBtn;
	import panel.viewSpots.StaticSlide;
	
	public class DragContainerBox extends Sprite
	{
		private var _width:int = 0;
		private var _height:int = 0;
		
		private var container:Sprite;
		
		private var currentTouchPointID:int = -999;
		
		private var contentList:Array = [];
		
		private var getMore:*;
		
		private var moveDirection:int = 0;
		
		private var _stage:Stage;
		
		public function DragContainerBox(wid:int,hei:int,stage:Stage,moreItem:* = null)
		{
			_width = wid;
			_height = hei;
			
			_stage = stage;
			
			getMore = moreItem;
			
			init();
		}
		
		private function init():void
		{
			var back:Shape = new Shape;
			back.graphics.beginFill(0x000000,.8);
			back.graphics.drawRect(0,0,_width,_height);
			back.graphics.endFill();
			
			this.addChild(back);
			this.mask = back;
			
			container = new Sprite;
			this.addChild(container);
			
			addEvent();
			
			//			if(ClientConstants.SLIDE_POS.memory)
			//			{
			//				container.y =  ClientConstants.SLIDE_POS.pos;
			//			}else
			//			{
			//			}
			container.y = 0;
		}
		
		public function addEvent():void
		{
			if(Multitouch.supportsTouchEvents){
				Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
				container.addEventListener(TouchEvent.TOUCH_BEGIN,onTouchBeginHandler);
				container.addEventListener(TouchEvent.TOUCH_END,onTouchEndHandler);
				
				//				container.addEventListener(TransformGestureEvent.GESTURE_SWIPE,onSwipHandler);               //现有输出类型冲突，暂时忽略掉滑动
			}
			
		}
		
		protected function onSwipHandler(event:TransformGestureEvent):void
		{
			if(event.offsetY > 0){
				moveDirection  = 1
			}else{
				moveDirection = -1;
			}
			
			startRoll();
		}
		
		private function startRoll():void
		{
			container.addEventListener(Event.ENTER_FRAME,moveHandler);
		}
		
		protected function moveHandler(event:Event):void
		{
			if(moveDirection == 1){
				if(container.y  < 0){
					container.y = container.y + moveDirection;
				}else{
					stopRoll();
				}
			}if(moveDirection == -1){
				if(container.y > _height - container.height){
					container.y = container.y + moveDirection;
				}else{
					stopRoll();
				}
			}
		}
		
		private function stopRoll():void
		{
			if(container.hasEventListener(Event.ENTER_FRAME)){
				container.removeEventListener(Event.ENTER_FRAME,moveHandler);
			}
		}
		
		protected function onEnd(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			container.stopDrag();
		}
		
		protected function onBegin(event:MouseEvent):void
		{
			stopRoll();
			event.stopImmediatePropagation();
			if(ClientConstants.SLIDE_POS.memory)
			{
				ClientConstants.SLIDE_POS.memory = false;
				container.startDrag(false,new Rectangle(0,_height - container.height,0, Math.abs(_height - container.height) + ClientConstants.SLIDE_POS.pos));
			}else{
				container.startDrag(false,new Rectangle(0,_height - container.height,0, Math.abs(_height - container.height)));
			}
		}
		
		public function clear():void
		{
			ClientConstants.SLIDE_POS = {pos:container.y};
			ClientConstants.SLIDE_POS.memory = false;
			
			container.removeEventListener(TouchEvent.TOUCH_BEGIN,onTouchBeginHandler);
			container.removeEventListener(TouchEvent.TOUCH_END,onTouchEndHandler);	
			clearAll();
			
			StaticSlide.clear(container);
			
			container = null;
			this.mask = null;
			getMore = null;
		}
		
		public function changeData(list:Array):void
		{
			clearAll();
			
			var locY:int = 0;
			for(var i:int = 0;i < list.length;i++){
				container.addChild(list[i]);
				list[i].x = 0;
				list[i].y = locY;
				locY = locY + list[i].height;
				
				contentList.push(list[i]);
			}
			
			//			if(_height < container.height){
			//				StaticSlide.beginSlide(container,_stage,new Rectangle(0,this.parent.y,0,-container.height));
			////				StaticSlide.beginSlide(container,_stage,new Rectangle(0,_height - container.height - _stage.stageHeight * .1,0,Math.abs(_height - container.height + _stage.stageHeight * .1)));
			//			}
			
			//			if(getMore != null){
			//				if(list.length < 10){
			//					return;
			//				}else{
			//					container.addChild(getMore);
			//					getMore.x = 0;
			//					getMore.y = locY;
			//				}
			//			}
			
		}
		
		private function clearAll():void
		{
			for(var i:int = 0;i < contentList.length;i++){
				container.removeChild(contentList[i]);
				contentList[i] = null;
			}
			contentList = [];
			if(getMore && getMore.parent){
				container.removeChild(getMore);
			}
		}
		
		protected function onTouchEndHandler(e:TouchEvent):void
		{
			container.stopTouchDrag(e.touchPointID);
			//			this.cacheAsBitmap = false;
		}
		
		protected function onTouchBeginHandler(e:TouchEvent):void
		{
			if(_height < container.height){
				container.startTouchDrag(e.touchPointID,false,new Rectangle(0,_height - container.height - _stage.stageHeight * .1,0, Math.abs(_height - container.height) + _stage.stageHeight * .1));                  //限定拖动范围，只能上下拖动
				//				this.cacheAsBitmap = true;
			}
		}
		
	}
}